package com.atguigu.yygh.order.service.impl;

import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.order.mapper.PaymentInfoMapper;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.order.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 支付信息表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-12-21
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderInfoService orderInfoService;

    //保存支付数据
    @Override
    public void savePaymentInfo(OrderInfo order, Integer paymentType) {
        //1.根据订单id和支付类型先去查询，如果有这条支付数据的话，就不能继续进行支付了
        //创建查询条件
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",order.getId());
        queryWrapper.eq("payment_type",paymentType);
        //进行数据查询
        Integer integer = baseMapper.selectCount(queryWrapper);
        //如果查询到的数量大于0的话，就证明是这条交易记录已经存在了
        if (integer > 0) {
            return;//如果存在，就直接返回方法
        }

        //封装要添加的交易记录的数据
        PaymentInfo paymentInfo = new PaymentInfo();

        //订单创建的时间
        paymentInfo.setCreateTime(new Date());
        //orderid
        paymentInfo.setOrderId(order.getId());
        //支付方式
        paymentInfo.setPaymentType(paymentType);
        //订单交易号
        paymentInfo.setOutTradeNo(order.getOutTradeNo());
        //订单状态，默认都是支付中，等支付完成了去改这个数据
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        //
        String subject = new DateTime(order.getReserveDate()).toString("yyyy-MM-dd")+"|"+order.getHosname()+"|"+order.getDepname()+"|"+order.getTitle();
        paymentInfo.setSubject(subject);
        //交易的金额
        paymentInfo.setTotalAmount(order.getAmount());

        //添加到数据库
        baseMapper.insert(paymentInfo);

    }


    @Override
    public void paySuccess(String outTradeNo, Integer paymentType, Map<String, String> paramMap) {
        //获取订单数据
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_trade_no",outTradeNo);
        OrderInfo orderInfo = orderInfoService.getOne(queryWrapper);

        //状态已经支付
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderInfo.setUpdateTime(new Date());
        orderInfoService.updateById(orderInfo);

        //获取交易记录数据
        //2 更新支付记录状态
        QueryWrapper<PaymentInfo> wrapperPayment = new QueryWrapper<>();
        wrapperPayment.eq("out_trade_no",outTradeNo);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapperPayment);
        //修改支付状态
        //设置状态
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        //transaction_id是非常重要的，是微信给我们返回的数据，在退款的时候是要用到
        paymentInfo.setTradeNo(paramMap.get("transaction_id"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramMap.toString());
        baseMapper.updateById(paymentInfo);

    }

    //获取支付订单详情
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {

        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        queryWrapper.eq("payment_type", paymentType);
        return baseMapper.selectOne(queryWrapper);
    }
}
